// pages/perCenter/disOrder/disOrder.js
var util = require('../../../utils/util.js');
var dateUtil = require('../../../utils/data.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();
Page({
  data: {
    // tab切换 
    currentTab: 0,
    // 页面配置  
    winWidth: 0,
    winHeight: 0,
    shareOrders:[],
    //订单状态 1-新订单（已支付) 5-进行中(商家已确认) 6-已完成（已完成) 2,3,4-已失效（取消，退款中，已退款)
    status:"1"
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      cur0:options.cur0,
      cur1: options.cur1,
      cur2: options.cur2,
      cur3: options.cur3
    })
    // 获取系统信息 
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    that.getData();
  },
  getData:function(){
    var that = this;
    var data={
      token:wx.getStorageSync("token"),
      status:that.data.status
    };
    util.request(api.ShareOrders, data).then(function (res) {
      if(res.code=='200'){
        var shareOrders = res.datas;
        var ids = "";
        for(var i=0;i<shareOrders.length;i++){
          var tempTime = shareOrders[i].createTime;
          var tempDate = new Date(tempTime);
          var tempTimeStr = dateUtil.formatDate(tempDate,"yyyy-MM-dd");
          shareOrders[i]['createTimeStr']=tempTimeStr;
          if(shareOrders[i].readStatus=='0'&&i==shareOrders.length-1){
            ids+=shareOrders[i].id;
          }else if(shareOrders[i].readStatus=='0'){
            ids+=shareOrders[i].id+",";
          }
        }
        var cur = 'cur' + that.data.currentTab;
        var data = {
          shareOrders: res.datas,
          curIds: ids
        };
        data[cur] = false;
        that.setData(data);
        that.fnReadOrder();
      } else if (res.code == '500' && res.datas =='authfaild'){
        wx.removeStorageSync("token");
        wx.showModal({
          title: '登录过时，请重新登录',
          content: '',
          success:function(options){
            if(options.confirm){
              wx.navigateTo({
                url: '/pages/auth/register/register',
              })
            }
          }
        })
      }
    });
  },
  // 滑动切换tab 
  bindChange: function (e) {
    var that = this;
    var current = e.detail.current;
    var status = that.data.status;
    if(current=='0'){
      status = '1';
    }else if(current =='1'){
      status = '5'
    }else if(current=='2'){
      status='6'
    }else{
      status='2,3,4'
    }
    that.setData({
       currentTab: current,
       status:status,
       shareOrders:[]
    });
    that.getData();
  },
  // 点击tab切换 
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
    }
  },
  fnReadOrder:function(e){
    var that = this;
    var curIds = that.data.curIds;
    if(curIds==''){
      return;
    }
    util.request(api.ReadOrder,{token:wx.getStorageSync("token"),ids:curIds}).then(function(res){
      if(res.code=="200"){

      }else{
        wx.showToast({
          title: '操作失败！!',
          duration: 1500,
          image: "/static/images/icon_error.png"
        })
      }
    })
    

  }
})